from flask import Blueprint, render_template, request, redirect, session,current_app
from ..utils import db

lg = Blueprint(name="login", import_name=__name__, url_prefix='/')




@lg.route("/login", methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template("login.html")
    else:
        username = request.form["user"]
        password = request.form["pwd"]
        print(username)
        user_dict = db.get_db_user(username, password)
        print(user_dict)
        if not user_dict:
            return render_template("login.html", msg="用户名或密码错误")
        session["user_info"] = user_dict
        session['has_seen_help'] = False
        session["population_size"] = current_app.config['DEFAULT_POPULATION_SIZE']
        session["n_generations"] = current_app.config['DEFAULT_N_GENERATIONS']
        session["max_capacity"] = current_app.config['DEFAULT_MAX_CAPACITY']
        session["mutation_rate"] = current_app.config['DEFAULT_MUTATION_RATE']
        return redirect("/optimize")

